/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.sigawe.gateway.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.sigawe.gateway.dao.interfc.IAccount;
import net.sigawe.gateway.model.Account;

/**
 *
 * @author Rahman
 */
public class AccountDAO implements IAccount {

    private Connection connection;

    public AccountDAO(Connection connection) {
        this.connection = connection;
    }

    @Override
    public boolean load(Account account) throws SQLException {
        boolean status = false;
        int data = 0;
        String sql = "SELECT nomor,username,password,ID,notelp FROM user WHERE username=? || password=? || ID=? || notelp=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, account.getUsername());
        ps.setString(2, account.getPassword());
        ps.setString(3, account.getID());
        ps.setString(4, account.getNotelp());
        ResultSet rs = ps.executeQuery();
        rs.last();
        data = rs.getRow();
        if (data != 0) {
            status = false;
        } else {
            status = true;
        }
        return status;

    }

    public void insert(Account account) throws SQLException {
      String sql = "INSERT INTO user (nomor,username,password,ID,notelp) VALUES(NULL,?,?,?,?)";
      PreparedStatement ps = connection.prepareStatement(sql);
      ps.setString(1, account.getUsername());
      ps.setString(2, account.getPassword());
      ps.setString(3, account.getID());
      ps.setString(4, account.getNotelp());
      ps.executeUpdate();
      ps.close();
    }

        
}
